/*
 ============================================================================
 Name		: SputnikBarDrawer.cpp
 Author	  : Den Grigorenko
 Copyright   : Copyright (c) 2008 Den123
 Description : CSputnikBarDrawer - allows to draw signal strength bars
 ============================================================================
 */


#include <aknutils.h> 

#include "SputnikBarDrawer.h"
#include "FontData.h" 

CSputnikBarDrawer :: ~CSputnikBarDrawer()
{
}

void CSputnikBarDrawer :: ConstructL( const CCoeControl* aParent, SFontData* aFontData, TPositionSatelliteInfo* aData )
{
   iFontData = aFontData;
   iData = aData;
   SetContainerWindowL( *aParent );		
   ActivateL();		   
}

const TInt KBarLbOffs = 5;
const TInt KMaxSignalStrength = 50;
void CSputnikBarDrawer :: Draw( const TRect& /*aRect*/ ) const
{
   TRect r = Rect();
   CWindowGc& gc = SystemGc();
   gc.SetBrushColor( KRgbWhite );    
   gc.Clear( r );
   if( iData->NumSatellitesInView() )
   {
      gc.UseFont( iFontData->iFont );
      
      TBool fromLeft2Right = r.Width() > r.Height();
      TInt gridSize = DrawGrid( gc, r );

      gc.SetPenSize( TSize( 1, 1 ) );
      gc.SetPenStyle( CGraphicsContext :: ESolidPen );
      gc.SetBrushStyle( CGraphicsContext :: ESolidBrush );
      
      // darw bar for each satellite    
      for( TInt i = 0; i < iData->NumSatellitesInView(); i++ )
      {
         // read satellite data
         TSatelliteData satData;
         iData->GetSatelliteData( i, satData );
         
 	 // create satellite label    
         TBuf<2> barLb;
         if( satData.SatelliteId() < 10 )
            barLb.AppendNum( 0 );
         barLb.AppendNum( satData.SatelliteId() );

 	 // calculate label position and bar rect    
         TPoint barLbPoint;
         TRect  barRect;
         if( fromLeft2Right )
         {
            barLbPoint.SetXY( KBarLbOffs + i * iFontData->iSatLbW, r.iBr.iY - 1 );
            barRect.SetRect(  barLbPoint.iX + 1, 
                              r.iTl.iY  + gridSize * ( KMaxSignalStrength - satData.SignalStrength() ) / KMaxSignalStrength,
                              barLbPoint.iX + iFontData->iSatLbW - 1, 
                              r.iTl.iY  + gridSize );
         }
         else
         {
            TInt yOffsFix = 1;
            barLbPoint.SetXY( r.iBr.iX  - iFontData->iSatLbW, KBarLbOffs + ( i + 1 ) * iFontData->iSatLbH );
            barRect.SetRect(  r.iTl.iX  + gridSize * ( KMaxSignalStrength - satData.SignalStrength() ) / KMaxSignalStrength, 
                              barLbPoint.iY - iFontData->iSatLbH + yOffsFix + 1, 
                              r.iTl.iX  + gridSize,  
                              barLbPoint.iY + yOffsFix );
         }  	        
 	
         // draw label    	  
         gc.SetPenColor( satData.IsUsed() ? iFontData->iUsedLbColor : iFontData->iUnusedLbColor );
         gc.DrawText( barLb, barLbPoint );

         // draw bar rect  	     
         gc.SetPenColor(   satData.IsUsed() ? iFontData->iUsedSatPenColor   : iFontData->iUnusedSatPenColor ); 
         gc.SetBrushColor( satData.IsUsed() ? iFontData->iUsedSatBrushColor : iFontData->iUnusedSatBrushColor ); 
         gc.DrawRect( barRect ); 	      	     
      } 	           
   }
}

// draw signal strength grid
TInt CSputnikBarDrawer :: DrawGrid( CWindowGc& aGc, const TRect& aRect ) const
{	
   aGc.SetPenColor( KRgbDarkGray );
   aGc.SetPenSize( TSize( 1, 1 ) );
   aGc.SetPenStyle( CGraphicsContext :: EDashedPen );
   TInt gridSize;
   TBool fromLeft2Right = aRect.Width() > aRect.Height();    
   if( fromLeft2Right )
   {
      // draw horizontal grid lines
      TPoint point1, point2; 
  	  
      point1.iX = aRect.iTl.iX;
      point1.iY = aRect.iTl.iY;
      point2.iX = aRect.iTl.iX + aRect.Width();
      point2.iY = aRect.iTl.iY;
      aGc.DrawLine( point1, point2 );
  	  
      for( TInt i = 0; i < 4; i ++ )
      {
         TSize sz2Move( 0, ( aRect.Height() - iFontData->iSatLbH ) / 4 );
         point1 += sz2Move;
         point2 += sz2Move;
         aGc.DrawLine( point1, point2 );
      }  	  
      gridSize = point1.iY - aRect.iTl.iY + 1;
    }
    else
    {
      // draw vertical grid lines
      TPoint point1, point2; 
  	  
      point1.iX = aRect.iTl.iX;
      point1.iY = aRect.iTl.iY;
      point2.iX = aRect.iTl.iX;
      point2.iY = aRect.iTl.iY + aRect.Height();
      aGc.DrawLine( point1, point2 );
      for( TInt i = 0; i < 4; i ++ )
      {
         TSize sz2Move( ( aRect.Width() - iFontData->iSatLbW ) / 4, 0 );
         point1 += sz2Move;
         point2 += sz2Move;
         aGc.DrawLine( point1, point2 );
      }    	  
      gridSize = point1.iX - aRect.iTl.iX + 1;
    }
    
    return gridSize;
}
